﻿/*
easyui　formatter　相关
叶燕鑫
2012-08-12
2014-02-08
V3V3
*/

/*1.json日期格式转换成 yyyy-MM-dd*/
function formatterdate(val, row) {
   
    if (val == null || val == undefined || val=="") {
        return "";
    }
    return val.substr(0, 10);
    var match;
    if (!(match = val.match(/\d+/))) {
        return "";
    }
    var dt = new Date();
    dt.setTime(match[0] - 0);
    var str = "";
    if (dt.getFullYear) {
        var y, m, d;
        y = dt.getFullYear();
        m = dt.getMonth() + 1; //1-12
        m = "" + m;
        d = "" + dt.getDate();
        if (m.length != 2) {
            m = "0" + m;
        }
        if (d.length != 2) {
            d = "0" + d;
        }
        str = "" + y + "-" + m + "-" + d;
    }
    return str;
}

/* 2.金额转换成用有千位符*/
function milliformatter(val, pos) {

    try {
        if (val == undefined)
            return "";
//        pos = pos == undefined ? 2 : pos;
//        var separator = ",";
//        var re = /(\d{3})\B/g;

//        var strtemp = "";
//        if ((val + "").indexOf(".") > 0) {
//           // return val;
//            var len = val.length
//            var splitpos = (val + "").indexOf(".");
//            strtemp = (val + "").substring(splitpos, len);
//            //strtemp;
//        }
//        val = val.toFixed(pos).toString();
        //        return val.replace(re, "$1" + separator) + strtemp;
        var s = val + "";
        var h = '';
        if (s.charAt(0) == '-') {
            h = '-';
            s = s.slice(1);
        }
        if (/[^0-9\.]/.test(s)) return "invalid value";
        s = s.replace(/^(\d*)$/, "$1.");
        s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
        s = s.replace(".", ",");
        var re = /(\d)(\d{3},)/;
        while (re.test(s)) s = s.replace(re, "$1,$2");
        s = s.replace(/,(\d\d)$/, ".$1");
        if (s.replace(/^\./, "0.") == "0.00")
            return "";
        else
            return h + s.replace(/^\./, "0.")
        
    }
    catch(e)
    {
        return val;
    }

   
}



/*===============================*/
//获取提交到服务器端的数据，[]
//id为数据所在DIV或其它容器HTML　ID
//cmd 为后台处理方法 以action= 为格式
function GetInputData(id, cmd) {
    var postdata = "{ \"action\":\"" + cmd + "\",";
    $("#" + id + " input[type!='checkbox']").each(function () {
        postdata += "\"" + $(this).attr("name") + "\":\"" + $(this).val() + "\",";
    });
    $("#" + id + " input[type='checkbox']").each(function () {
        postdata += "\"" + $(this).attr("name") + "\":\"" + this.checked + "\",";
    });
    postdata = postdata.substr(0, postdata.length - 1);
    postdata += "}";
    return eval("(" + postdata + ")");
}


/*序列化指定内容*/
$.fn.serializeJSON = function (type) {
    // var str = $(this).serialize();

    var str = $('input, select, textarea', this).filter(function () {
        return this.name && !this.disabled &&
				(this.checked || /select|textarea/i.test(this.nodeName) ||
					/text|hidden|password/i.test(this.type));
    }).serialize();
    str = str.replace(/&/g, "','");
    str = str.replace(/=/g, "':'");
    str = "({'" + str + "'})";
    var obj = eval(str);
    return obj;



};
/*序列化指定内容*/
//(function ($) {
//    $.fn.serializeJson = function () {
//        var serializeObj = {};
//        var array = this.serializeArray();
//        var str = this.serialize();
//        $(array).each(function () {
//            if (serializeObj[this.name]) {
//                if ($.isArray(serializeObj[this.name])) {
//                    serializeObj[this.name].push(this.value);
//                } else {
//                    serializeObj[this.name] = [serializeObj[this.name], this.value];
//                }
//            } else {
//                serializeObj[this.name] = this.value;
//            }
//        });
//        return serializeObj;
//    };
//})(jQuery);

$.extend($.fn.datagrid.defaults.editors, {
    datebox: {
        init: function (container, options) {
            var input = $('<input type="text">').appendTo(container);
            input.datebox(options);
            return input;
        },
        destroy: function (target) {
            $(target).datebox('destroy');
        },
        getValue: function (target) {
            return $(target).datebox('getValue');
        },
        setValue: function (target, value) {
            $(target).datebox('setValue', formatterdate(value));
        },
        resize: function (target, width) {
            $(target).datebox('resize', width);
        }
    }
});